function matrix_role_subject() {
    var _this = this;
    var page;
    _this.index = -1;
    var items;
    _this.init = function (page_person) {
        page = page_person;
        _this.initTable();
        _this.initEvent();
    };
    //按钮事件
    _this.initEvent = function () {

        var subjectIdAll = [];
        var object = {};
        /**
         * 全选事件
         */
        $("#checkAll").click(function () {
                if (this.checked) {
                    $("#trainSubject_table :checkbox:not(:disabled)").prop("checked", true);

                    $("#trainSubject_table :checkbox:not(:disabled)").each(function () {
                        if (!object[$(this).val()]) {
                            object[$(this).val()] = 1;
                            subjectIdAll.push($(this).val());
                        }
                    });
                } else {
                    $("#trainSubject_table :checkbox:not(:disabled)").prop("checked", false);
                    if (subjectIdAll.length > 0) {
                        for (var a = 0; a < subjectIdAll.length; a++) {
                            $("#trainSubject_table :checkbox:not(:disabled)").each(function () {
                                if ($(this).val() === subjectIdAll[a]) {
                                    delete object[$(this).val()];
                                    subjectIdAll.splice(a, 1);
                                }
                            })
                        }
                    }
                }
            }
        );

        /**
         * 单选事件
         */
        $("#trainSubject_table").on("click", "input[type=checkbox]", function () {
            if ($(this).is(":checked")) {
                if (!object[$(this).val()]) {
                    object[$(this).val()] = 1;
                    subjectIdAll.push($(this).val());
                }
            } else {
                if (subjectIdAll.length > 0) {
                    for (var a = 0; a < subjectIdAll.length; a++) {
                        if ($(this).val() === subjectIdAll[a]) {
                            delete object[$(this).val()];
                            subjectIdAll.splice(a, 1);
                        }
                    }
                }
            }
        });

        //删除按钮
        $("#btn_del").click(function () {
            if (subjectIdAll.length===0) {
                layer.alert("请选择要删除的主题",{icon:7});
                return;
            }
            _this.index = layer.alert('确定要删除关联主题?', {
                btn: ['确定', '取消'],
                icon:3,
                btn1: function () {
                    var params = {
                        subjectIds: subjectIdAll.toString(),
                        trainRoleId: $("#trainRoleId").val()
                    };
                    console.log(params)
                    $.ajax({
                        url: appPath + "/admin/matrix/deleteMatrix",
                        data: params,
                        dataType: "json",
                        type: "POST",
                        success: function (data) {
                            var code = data.code;
                            if (10000 == code) {
                                layer.msg("删除成功",{icon:1,time:1000},function () {
                                    // 清空选中
                                    subjectIdAll = [];
                                    object = {};
                                    _this.initTable();
                                });
                            } else {
                                layer.msg("删除失败",{icon:2})
                            }
                        }
                    });
                }
            })
        });

        // 关联更多主题
        $("#btn_add").click(function () {
            // 调用选择培训方案的弹窗
            layer.open({
                type : 2,
                title : '选择培训方案',
                area : [ '900px', '600px' ],		//弹出层大小
                scrollbar : false,				//false隐藏滑动块
                content : [ appPath + '/admin/matrix/selectSubject' +
                '?trainRoleId='+$("#trainRoleId").val()+'&trainRoleName='+$("#trainRoleName").val(), 'yes' ]
            });
        });
    }

    _this.initTable = function () {
        var list_url = appPath + "/admin/matrix/subjectList";
        page.init("trainSubject_form", list_url, "trainSubject_table", "trainSubject_page", 1, 10);
        page.goPage(1);
        page.list = function (dataList) {
            $("#all").attr("checked", false);
            var len = dataList.length;
            items = dataList;
            var inner = "", item;
            for (var i = 0; i < len; i++) {
                // 组装数据
                item = dataList[i];
                inner += '<tr>';
                inner += '<td><input type="checkbox" name="ckbox"  value="' + item.intTrainSubjectId + '" /></td>';
                inner += '<td>';
                inner += '<span>' + item.varSubjectName + '</span>';
                inner += '</td>';
                var temp_logo = appPath + "/static/global/images/subject_image/subject_image_" + parseInt(Math.round(Math.random())) + ".png";
                if ("" == item.logo || null == item.logo) {
                    inner += '<td><img width="100" height="100" src="' + temp_logo + '" alt=""></td>';
                } else {
                    inner += '<td><img data-original="' + temp_logo + '" width="100" height="100" src=' + item.logo + ' alt="" onerror="train_subject.imgError(this)"></td>';
                }
                inner += '<td>' + item.courseCount + '</td>';
                inner += '<td>' + parseFullDate(item.datOperDate) + '</td>';
                inner += '<td><a href="javascript:matrix_role_subject.info(\'' + item.intTrainSubjectId + '\');"  id="btn_all" class="a a-info">详情</a> ';

                inner += '</tr>';
            }
            return inner;
            console.log(inner)
        }
    };

    // 搜索
    _this.search = function () {
        _this.initTable();
    };

    // 全部
    _this.searchAll = function () {
        $("#trainSubject_form").find("input[id=subjectName]").val("");
        _this.initTable();
    };

    // 主题详情
    _this.info = function (varId) {
        window.open(appPath + "/admin/trainSubject/trainSubjectInfo?intTrainSubjectId=" + varId, "_blank");
        // window.location.href = appPath + "/admin/trainSubject/trainSubjectInfo?varId=" + varId;
    }


    _this.ajax = function (url, param, type) {
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function (data) {
                result = data;

            }
        });
        return result;
    };


    function parseFullDate(str) {
        if(!str) return "";
        if(str.length<=10) return "";
        return str.substring(0,10);
    }
}

var matrix_role_subject = new matrix_role_subject();



